package com.besttone.locate.util;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import com.besttone.locate.model.PosInfoModel;

public class PosInfoHandler extends DefaultHandler {
	private boolean isPosInfo = false;
	private boolean xPOSITIONRESULT = false;
	private boolean xMSID = false;
	private boolean xLATITUDE = false;
	private boolean xLONGITUDE = false;
	private boolean xROAMCITY = false;
	private boolean isError = false;
	private boolean xErrorCode = false;
	private boolean xErrorMsg = false;
	private PosInfoModel posInfo;
	
	/**导航到开始标签触发**/
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) {
		String tagName = localName.length() == 0 ? qName : localName;
		tagName = tagName.toUpperCase().trim();
		//如果读取的是posInfo标签开始，则实例化posInfo
		if (tagName.equals("POSINFO")) {
			isPosInfo = true;
			posInfo = new PosInfoModel();
		}
		if (tagName.equals("ERROR")) {
			isError = true;
			posInfo = new PosInfoModel();
		}
		//然后读取其他节点
		if (isPosInfo) {
			if (tagName.equals("POSITIONRESULT"))
				xPOSITIONRESULT = true;
			if (tagName.equals("MSID"))
				xMSID = true;
			if (tagName.equals("LATITUDE"))
				xLATITUDE = true;
			if (tagName.equals("LONGITUDE"))
				xLONGITUDE = true;
			if (tagName.equals("ROAMCITY"))
				xROAMCITY = true;
		}
		if (isError) {
			if (tagName.equals("ERRORCODE"))
				xErrorCode = true;
			if (tagName.equals("ERRORMESSAGE"))
				xErrorMsg = true;
		}
	}
	
	/**导航到结束标签触发**/
	public void endElement(String uri, String localName, String qName) {
		String tagName = localName.length() == 0 ? qName : localName;
		tagName = tagName.toUpperCase().trim();
		 //如果读取的是posInfo标签结束
		if(tagName.equals("POSINFO")){
			isPosInfo = true;
		}
		//然后读取其他节点
		if (isPosInfo) {
			if (tagName.equals("POSITIONRESULT"))
				xPOSITIONRESULT = false;
			if (tagName.equals("MSID"))
				xMSID = false;
			if (tagName.equals("LATITUDE"))
				xLATITUDE = false;
			if (tagName.equals("LONGITUDE"))
				xLONGITUDE = false;
			if (tagName.equals("ROAMCITY"))
				xROAMCITY = false;
		}
		 //如果读取的是error标签结束
		if(tagName.equals("ERROR")){
			isError = true;
		}
		if (isError) {
			if (tagName.equals("ERRORCODE"))
				xErrorCode = false;
			if (tagName.equals("ERRORMESSAGE"))
				xErrorMsg = false;
		}
	}
	
	//这里是读取到节点内容时候回调
	public void characters(char ch[], int start, int length) {
		// 设置属性值
		if(isError)
			posInfo.setErrorFlag(true);
		if (xErrorCode)
		// 解决null问题
		posInfo.setErrorCode(posInfo.getErrorCode() != null ? (new StringBuilder(
				String.valueOf(posInfo.getErrorCode()))).append(
				new String(ch, start, length)).toString() : "");
		if (xErrorMsg)
			// 解决null问题
			posInfo.setErrorMsg(posInfo.getErrorMsg() != null ? (new StringBuilder(
					String.valueOf(posInfo.getErrorMsg()))).append(
					new String(ch, start, length)).toString() : "");

		if (xPOSITIONRESULT)
			// 解决null问题
			posInfo.setPOSITIONRESULT(posInfo.getPOSITIONRESULT() != null ? (new StringBuilder(
					String.valueOf(posInfo.getPOSITIONRESULT()))).append(
					new String(ch, start, length)).toString() : "");
		if (xMSID)
			posInfo.setMSID(posInfo.getMSID() != null ? (new StringBuilder(
					String.valueOf(posInfo.getMSID()))).append(
					new String(ch, start, length)).toString() : "");
		if (xLATITUDE)
			posInfo.setLATITUDE(posInfo.getLATITUDE() != null ? (new StringBuilder(
					String.valueOf(posInfo.getLATITUDE()))).append(
					new String(ch, start, length)).toString() : "");
		if (xLONGITUDE)
			posInfo.setLONGITUDE(posInfo.getLONGITUDE() != null ? (new StringBuilder(
					String.valueOf(posInfo.getLONGITUDE()))).append(
					new String(ch, start, length)).toString() : "");
		if (xROAMCITY)
			posInfo.setMSID(posInfo.getROAMCITY() != null ? (new StringBuilder(
					String.valueOf(posInfo.getROAMCITY()))).append(
					new String(ch, start, length)).toString() : "");
	}
	
	public PosInfoModel getPosInfo() {
		return posInfo;
	}
}
